package cn.uce.cc.srv.biz.impl;

import cn.uce.base.PubApiObjectConstants;
import cn.uce.base.ccos.*;
import cn.uce.cc.api.service.biz.ICcosRoutTrackBiz;
import cn.uce.cc.srv.client.CcosInterfaceClient;
import cn.uce.cc.srv.utils.HttpClientPubApiUtils;
import cn.uce.cc.srv.utils.ScreenBusiConditionUtils;
import cn.uce.common.omg.cache.FspDictDateCache;
import cn.uce.common.utils.DateUtil;
import cn.uce.ics.base.entity.cs.IcsFspDictDataEntity;
import cn.uce.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 路由与物流轨迹接口
 * @Author liupan
 * @Description
 * @Date  2020-07-01 11:37
 */
@Service("ccosRoutTrackBiz")
public class CcosRoutTrackBiz implements ICcosRoutTrackBiz {

    private Logger log = LoggerFactory.getLogger(CcosRoutTrackBiz.class);
    @Resource
    CcosInterfaceClient ccosInterfaceClient;

    @Resource
    private FspDictDateCache fspDictDateCache;


    /**
     * 对话框查询路由时效
     * @param map
     * @return
     */
    @Override
    public String routDetilInfo(Map<String, Object> map) {
        String routPrescriptionInfo = "";
        String msg_temp = HttpClientPubApiUtils.buildUpStructure(PubApiObjectConstants.ROUTE_TIME_QUERY_BY_WAYBILL_NO,
                map);
        try {
            log.warn("routDetilInfo,begin"+JSON.toJSONString(map));
            routPrescriptionInfo = ccosInterfaceClient.routDetilInfo(msg_temp);
            log.warn("routDetilInfo,end"+routPrescriptionInfo);
            RoutTrakstandardVo routTrakVo = new RoutTrakstandardVo();
            if (map.isEmpty() || StringUtil.isEmpty(routPrescriptionInfo))
                return "";
            JSONObject jSONObject = JSONObject.parseObject(routPrescriptionInfo);
            String str_api_temp = jSONObject.getString("data");
            JSONObject jSONObject_temp = JSONObject.parseObject(str_api_temp);
            String dayNum = jSONObject_temp.getString("dayNum");
            String hourNum = jSONObject_temp.getString("hourNum");
            String routeName = jSONObject_temp.getString("routeName");
            routTrakVo.setRouteTime(dayNum + "天" + hourNum + "时");
            if (StringUtil.isBlank(hourNum) &&  StringUtil.isBlank(dayNum))
                routTrakVo.setRouteTime("无");
            routTrakVo.setStandardTime(routeName);
            if (StringUtil.isBlank(routeName))
                routTrakVo.setStandardTime("无");
            List routTimeList = JSONObject.parseArray(jSONObject_temp.getString("lineVos"));
            if (StringUtil.isNotEmpty(jSONObject_temp.getString("lineVos"))) {
                List<RoutTimeListVo> list = new ArrayList<RoutTimeListVo>();
                for (Object obj : routTimeList) {
                    RoutTimeListVo routTimeVo = new RoutTimeListVo();
                    String lineVo_str = obj.toString();
                    LineVo lineVo = JSONObject.parseObject(lineVo_str, LineVo.class);
                    if (StringUtil.isNotEmpty(lineVo.getStartTime()))
                        routTimeVo.setPlanGoTime(lineVo.getStartTime());
                    if (StringUtil.isNotEmpty(lineVo.getEndTime()))
                        routTimeVo.setPlanArrivalTime(lineVo.getEndTime());
                    if (StringUtil.isNotEmpty(lineVo.getLineCode()))
                        routTimeVo.setLineCode(lineVo.getLineCode());
                    if (StringUtil.isNotEmpty(lineVo.getLineName()))
                        routTimeVo.setLineName(lineVo.getLineName());
                    if (StringUtil.isNotEmpty(lineVo.getSourceZoneName()))
                        routTimeVo.setOutsetDep(lineVo.getSourceZoneName());
                    if (StringUtil.isNotEmpty(lineVo.getDestZoneName()))
                        routTimeVo.setArrivalDep(lineVo.getDestZoneName());
                    if (StringUtil.isNotEmpty(lineVo.getColumn1()))
                        routTimeVo.setOperateTime(lineVo.getColumn1());
                    list.add(routTimeVo);
                }
                routTrakVo.setRoutTimeList(list);
            }
            log.warn("routDetilInfo,result"+routTrakVo);
            return JSON.toJSONString(routTrakVo);
        } catch (Exception e) {
            log.error("银河查询路由时效出现异常：" + e);
        }
        return null;
    }

     /**
       * 查询物流轨迹详情
       * @Author liupan
       * @Description 
       * @Date  2020-07-01 13:42
       */
    @Override
    public LinkedList<RoutTrackVo> getRoutingLocusInfoByWBNo(String wayNum) {
        LinkedList<RoutTrackVo>  routingLocus = new LinkedList<>();
        Map map = new HashMap();
        map.put("wayBillNo", wayNum);
        String jsonObject = HttpClientPubApiUtils.buildUpStructure(PubApiObjectConstants.ROUTE_QUERY_BY_WAYBILL_NO,  map);
        try {
            log.warn("getRoutingLocusInfoByWBNo,begin"+jsonObject);
            String routingLocu1s = ccosInterfaceClient.getRoutingLocusInfoByWBNo(jsonObject);
            log.warn("getRoutingLocusInfoByWBNo,end"+routingLocu1s);
            routingLocus =   converterQueryRoute(routingLocu1s);
        } catch (Exception e) {
            log.error("银河查询路由信息出现异常：" + e);
        }
        return routingLocus;
    }



    /**
     * 获取产品类型
     * @return
     */
    @Override
    public List<IcsFspDictDataEntity> getrouttrakType() {
        List<IcsFspDictDataEntity> waybill_protype = fspDictDateCache.getFspDictForTypeCode("waybill_protype");
        log.warn("getrouttrakType,begin"+JSON.toJSONString(waybill_protype));
        return waybill_protype;
    }


    /**
       * 对物流轨迹进行解析
       * @Author liupan
       * @Description
       * @Date  2020-07-01 13:42
       */
    public LinkedList<RoutTrackVo> converterQueryRoute(String routingLocusInfo)   {
        if (!ScreenBusiConditionUtils.isEmptyForApi(routingLocusInfo, "data")) {
            return null;
        }
        RoutTrackVo routTrackVo = null;
        LinkedList<RoutTrackVo> trackList = new LinkedList();
        JSONObject jSONObject = JSONObject.parseObject(routingLocusInfo);
        String str_api_temp = jSONObject.getString("data");
        if (!ScreenBusiConditionUtils.isEmptyForApi(routingLocusInfo, "data")) {
            return null;
        }
        List lst = JSONObject.parseArray(str_api_temp);
        for (Object obj : lst) {
            String str_obj_temp = obj.toString();
            WaybillTrackVo waybillTrackVo = JSONObject.parseObject(str_obj_temp, WaybillTrackVo.class);
            routTrackVo = new RoutTrackVo();
            if (StringUtil.isNotEmpty(waybillTrackVo.getOperDeptName())) {
                routTrackVo.setOperateDep(waybillTrackVo.getOperDeptName());
            }
            if (StringUtil.isNotEmpty(waybillTrackVo.getDestDeptName())) {
                routTrackVo.setNextDep(waybillTrackVo.getDestDeptName());
            }
            if (null != waybillTrackVo.getOperTypeCode()) {
                routTrackVo.setOperateTypeName(fspDictDateCache.getTypeNmaeForTypeCode("rout_operatetype",  waybillTrackVo.getOperTypeCode().toString()));
            }
            if (StringUtil.isNotEmpty(waybillTrackVo.getOperatorName())) {
                routTrackVo.setOperater(waybillTrackVo.getOperatorName());
            }
            if (null != waybillTrackVo.getOperateTime()) {
                routTrackVo.setOperateTime(DateUtil.dateToString(waybillTrackVo.getOperateTime(), "yyyy-MM-dd HH:mm"));
            }
            if (null != waybillTrackVo.getPiceNum()) {
                routTrackVo.setScanNum(String.valueOf(waybillTrackVo.getPiceNum()));
            }
            trackList.addFirst(routTrackVo);
        }
        return trackList;
    }
}
